package com.small.view.listviewimage;

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.small.R;
import com.small.model.DzModel;

public class ItemAdapter extends BaseAdapter{
	
	private LayoutInflater mInflater;
	private Vector<DzModel> mModels = new Vector<DzModel>();
	private ListView mListView;
	SyncImageLoader syncImageLoader;
	Context co;
	public ItemAdapter(Context context,ListView listView,Vector<DzModel> _mModels){
		mInflater = LayoutInflater.from(context);
		syncImageLoader = new SyncImageLoader();
		mListView = listView;
		mModels=_mModels;
		co=context;
		mListView.setOnScrollListener(onScrollListener);
	}

	public void clean(){
		mModels.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mModels.size();
	}

	@Override
	public Object getItem(int position) {
		if(position >= getCount()){
			return null;
		}
		return mModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.dzhome_list_item, null);
		}
		DzModel model = mModels.get(position);
		convertView.setTag(position);
		ImageView iv = (ImageView) convertView.findViewById(R.id.imte_img);
		TextView sItem_Title =  (TextView) convertView.findViewById(R.id.item_name);
		TextView sItem_detail =  (TextView) convertView.findViewById(R.id.item_detail);
		TextView sItem_addr =  (TextView) convertView.findViewById(R.id.item_addr);
		
		sItem_Title.setText(model.getShopFirstName());
		sItem_addr.setText("地址:"+model.getShopAddress());
		sItem_detail.setText("距离:"+model.getDistance()+"  人气:"+model.getViewTimes());
		
		ImageButton btn_gouwc=(ImageButton)convertView.findViewById(R.id.item_on);
		btn_gouwc.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(co, "ok", Toast.LENGTH_SHORT).show();
			}
		});
		
		iv.setBackgroundResource(R.drawable.list_img_load);
		try {
			syncImageLoader.loadImage(position,model.getSmallImageUrl(),imageLoadListener);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return  convertView;
	}

	SyncImageLoader.OnImageLoadListener imageLoadListener = new SyncImageLoader.OnImageLoadListener(){

		@Override
		public void onImageLoad(Integer t, Drawable drawable) {
			//BookModel model = (BookModel) getItem(t);
			View view = mListView.findViewWithTag(t);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.imte_img);
				iv.setBackgroundDrawable(drawable);
			}
		}
		@Override
		public void onError(Integer t) {
			DzModel model = (DzModel) getItem(t);
			View view = mListView.findViewWithTag(model);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.imte_img);
				iv.setBackgroundResource(R.drawable.list_img_load);
			}
		}
		
	};
	
	public void loadImage(){
		int start = mListView.getFirstVisiblePosition();
		int end =mListView.getLastVisiblePosition();
		if(end >= getCount()){
			end = getCount() -1;
		}
		syncImageLoader.setLoadLimit(start, end);
		syncImageLoader.unlock();
	}
	
	AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
				case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
					syncImageLoader.lock();
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
					loadImage();
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					syncImageLoader.lock();
					break;
				default:
					break;
			}
			
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			
		}
	};
}
